package com.kungfuhacking.wristbandpro.my.activity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.location.activity.AddSNCodeActivity;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.my.bean.CreateBean;
import com.kungfuhacking.wristbandpro.net.BaseNoTObserver;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;
import com.kungfuhacking.wristbandpro.utils.Constant;
import com.kungfuhacking.wristbandpro.widget.ActionSheetDialog;
import com.kungfuhacking.wristbandpro.widget.MyDatePicker;

import java.util.Calendar;

import io.reactivex.disposables.Disposable;

/**
 * 创建新空间
 * Created by Aaron on 17/5/16.
 */

public class CreateSpaceActivity extends BaseActivity implements View.OnClickListener {

    private EditText etBirthday;
    private EditText etSex;
    private EditText etName;
    private Disposable disposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_create_space);
        initWidget();

    }

    private void initWidget() {

        etBirthday = (EditText) findViewById(R.id.etBirthday);
        etSex = (EditText) findViewById(R.id.etSex);
        etName = (EditText) findViewById(R.id.etName);
        TitleBarView tbv = (TitleBarView) findViewById(R.id.tbv);
        etSex.setOnClickListener(this);
        etBirthday.setOnClickListener(this);
        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tbv.setOnRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkInfo();
            }
        });
    }

    public void checkInfo() {

        if (TextUtils.isEmpty(etName.getText().toString())) {
            showErr("请输入老人昵称");
            return;
        }

        if (TextUtils.isEmpty(etSex.getText().toString())) {
            showErr("请选择老人性别");
            return;
        }

        if (TextUtils.isEmpty(etBirthday.getText().toString())) {
            showErr("请选择老人生日");
            return;
        }

        createSpace();
    }

    /**
     * 创建空间
     */
    private void createSpace() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("birthday", etBirthday.getText().toString());
        j.put("name", etName.getText().toString());
        j.put("sex", etSex.getText().toString());

        RetrofitHttpUtil.getInstance().createChild(new BaseNoTObserver<CreateBean>() {
            @Override
            public void onHandleSuccess(CreateBean basebean) {
                hideLoading();
                showErr("创建成功");
                //把当前添加的老人 设置默认的老人
                sp.edit().putString(Constant.CURRENTOLDID, basebean.getId()).apply();
                sp.edit().putString(Constant.CURRENTOLDNAME, etName.getText().toString()).apply();
                nextActivityThenKill(AddSNCodeActivity.class);

            }

            @Override
            public void onHandleError(String message) {
                hideLoading();
                showErr(message);
            }

            @Override
            public void onReLogin(String message) {
                hideLoading();
                showErr(message);
                nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {
                disposable = d;
                showLoading("创建中...");
            }
        }, new RequestBean().setData(j.toString()));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.etSex:
                showChoose();
                break;

            case R.id.etBirthday:
                chooseBirthday();
                break;
        }
    }

    /**
     * 选择生日
     */
    private void chooseBirthday() {
        Calendar c = Calendar.getInstance();

        new MyDatePicker(this, new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {

                String month = (monthOfYear + 1) < 10 ? "0" + (monthOfYear + 1) : (monthOfYear + 1) + "";
                String day = dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth + "";
                etBirthday.setText(year + "-" + month + "-" + day);

            }

        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
                c.get(Calendar.DAY_OF_MONTH)).show();
    }

    /**
     * 弹出选择照片的对话框
     */
    @Deprecated
    protected void showChoose() {

        new ActionSheetDialog(this)
                .builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem("男", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                etSex.setText("男");
                            }

                        })
                .addSheetItem("女", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                etSex.setText("女");
                            }
                        }).show();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null)
            disposable.dispose();
    }
}
